package com.ziyia.wim.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.ziyia.wim.R;
import com.ziyia.wim.activity.BaseActivity;
import com.ziyia.wim.activity.Wim;
import com.ziyia.wim.adapter.ViewPagerAdapter;
import com.ziyia.wim.util.HttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class IndexFragment extends Fragment {
    private List<Map> groups = new ArrayList<>();

    public String uid, username;

    public IndexFragment() {}


    public static InfoFragment newInstance(String param1, String param2) {
        InfoFragment fragment = new InfoFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            Bundle arguments = getArguments();
            uid = arguments.getString("uid");
            username = arguments.getString("username");

            String url = "http://" + BaseActivity.IP + "/group/groups";
            HttpUtil.get(url, new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {

                }

                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                    String body = response.body().string();
                    List<Map> maps = JSON.parseArray(body, Map.class);
                    Log.d("艹", maps.toString());
                    groups.addAll(maps);
                }
            });
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_1, container, false);

        RecyclerView recyclerView = view.findViewById(R.id.list);
        // 线性布局
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        ViewPagerAdapter myAdapter = new ViewPagerAdapter(groups, getContext());
        recyclerView.setAdapter(myAdapter);

        myAdapter.setOnRecyclerViewClickListener(new ViewPagerAdapter.OnRecyclerViewClickListener() {
            @Override
            public void onRecyclerViewClick(int position) {
                Map map = groups.get(position);
                Long i = (Long) map.get("id");
                String gid = String.valueOf(i);
                String group = (String) map.get("name");
                actionStart(getActivity(), uid, gid, group, username);
            }
        });

        return view;

    }

    private void actionStart(Context context, String uid, String gid, String group, String username) {
        Intent intent = new Intent(context, Wim.class);
        intent.putExtra("uid", uid);
        intent.putExtra("gid", gid);
        intent.putExtra("group", group);
        intent.putExtra("username", username);
        context.startActivity(intent);
    }

}